package com.deam.scorpio.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.deam.scorpio.R;
import com.deam.scorpio.ui.dialog.LoadingDialog;
import com.deam.scorpio.ui.util.UrlAddress;
import com.qbkj.base_lib.ui.BaseActivity;
import com.zhy.http.okhttp.BaseBean;
import com.zhy.http.okhttp.HttpArrayCallback;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;

public class RegisterActivity extends BaseActivity {
    private LoadingDialog mLoadingDialog;

    @Override
    protected int setContentLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        v.setText(R.id.title_text, "注册帐号");
        v.setOnClickListener(this, R.id.reg_btn);
        mLoadingDialog = new LoadingDialog(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.reg_btn:
                http();
                break;
        }
    }

    private void http() {
        final String phone = v.getEditText(R.id.user_edit).getText().toString();
        String name = v.getEditText(R.id.name_edit).getText().toString();
        if (phone.length() == 0) {
            toast("请输入正确的手机");
            return;
        } else if (name.length() == 0) {
            toast("请输入正确的昵称");
            return;
        }
        mLoadingDialog.show();
        Map map = new HashMap();
        map.put("phone", phone);
        map.put("netname", name);
        OkHttpUtils.post().url(UrlAddress.URL_REGISTER).params(map).build().execute(new HttpArrayCallback<BaseBean>(this) {
            @Override
            protected void onSuccess(BaseBean data) {
                mLoadingDialog.dismiss();
                Intent intent = new Intent();
                intent.putExtra("phone", phone);
                setResult(1000, intent);
                finish();
            }

            @Override
            protected void onFail(int code, String msg) {
                toast(msg);
                mLoadingDialog.dismiss();
            }
        });
    }
}
